<?
//////////////////////////////////////////////////////////////////////////////////////////////////////////

function generate_invoice_number($arr){
	global $p1, $p2, $p3, $p4;
	
	if($arr['client_vat_num']){
		$vat = 'vat';
	}else{
		$vat = 'novat';
	}
	
	$result[0] = $p1[$arr['invoice_type']];
	$result[1] = $p2[$arr['payment_method']];
	$result[2] = $p3['else'];
	$result[3] = $p4 + $arr['order_id'];
	$result[4] = 'x'.$arr['index'];

	foreach($p3 as $k => $v){
		if(is_array($v) and in_array($arr['client_country'], $v['in_country'])){
			$result[2] = $v[($vat)];
			break;
		}
	}
	
	ksort($result);
	return (implode('-', $result));      
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////

function microtime_float(){
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////

function getByMoth($type, $ret = ''){
	$tmp_selector_tables = $GLOBALS['selector']->tables;
	$params['reseller_id'] = $GLOBALS['id'];
	$params['client_id'] = '^IS NOT NULL';
	$options['order_by_'] = 'DESC';
	$options['limit'] = 6;
	$options['offset'] = 0;

	if($type == 'cn'){
		$GLOBALS['selector']->tables = $GLOBALS['select_tables3'];
		$params['order_status'] = array('1', '2', '4', '8', '64', '1024');
		$params['sale_type'] = "^='reversal'";
		//$params['sale_date__'] = "^>'".date('Y-m', mktime(0, 0, 0, date('m')-20, 0, date('Y')))."'";
		$options['group_by'] = array('count', 'sale_date__');
		$options['order_by'] = 'sale_date__';				
	}elseif($type == 'inv'){
		$GLOBALS['selector']->tables = $GLOBALS['select_tables1'];
		$params['order_status'] = array('1', '2', '4', '8', '16', '32', '64', '1024');
		//$params['order_date__'] = "^>'".date('Y-m', mktime(0, 0, 0, date('m')-20, 0, date('Y')))."'";
		$options['group_by'] = array('count', 'order_date__');
		$options['order_by'] = 'order_date__';
	}
	
	$tmp_res = $GLOBALS['selector']->get_data($params, $options, $ret);
	$GLOBALS['selector']->tables = $tmp_selector_tables;

	//if(me() and $type=='cn'){print_r($GLOBALS['selector']->get_data($params, $options, 'query'));}
	if($ret == 'query'){
		return $tmp_res;
	}
	
	foreach($tmp_res['result'] as $k => $v){
		$res[$v[$options['order_by']]] = $v;
	}
		
	return $res;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////

function getByYear($type, $ret = ''){
	$tmp_selector_tables = $GLOBALS['selector']->tables;
	$params['reseller_id'] = $GLOBALS['id'];
	$params['client_id'] = '^IS NOT NULL';
	$options['order_by_'] = 'DESC';

	if($type == 'cn'){
		$GLOBALS['selector']->tables = $GLOBALS['select_tables3'];
		$params['order_status'] = array('1', '2', '4', '8', '64', '1024');
		$params['sale_type'] = "^='reversal'";
		$options['group_by'] = array('count', 'sale_date___');
		$options['order_by'] = 'sale_date___';		
	}elseif($type == 'inv'){
		$GLOBALS['selector']->tables = $GLOBALS['select_tables1'];
		$params['order_status'] = array('1', '2', '4', '8', '16', '32', '64', '1024');
		$options['group_by'] = array('count', 'order_date___');
		$options['order_by'] = 'order_date___';
	}
	
	$tmp_res = $GLOBALS['selector']->get_data($params, $options, $ret);
	$GLOBALS['selector']->tables = $tmp_selector_tables;
	
	if($ret == 'query'){
		return $tmp_res;
	}
	foreach($tmp_res['result'] as $k => $v){
		$res[$v[$options['order_by']]] = $v;
	}	
	
	return $res;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////

function getByCurr($type, $params = array(), $options = array(), $ret = ''){
	$res = array();
	$tmp_selector_tables = $GLOBALS['selector']->tables;
	$params1['reseller_id'] = $GLOBALS['id'];
	$params1['client_id'] = '^IS NOT NULL';
	$options1['order_by'] = 'payment_currency';
	$options1['order_by_'] = 'DESC';
	$options1['group_by'] = array('count', 'payment_currency');
	
	if($type == 'cn'){
		$GLOBALS['selector']->tables = $GLOBALS['select_tables3'];
		$params1['order_status'] = array('1', '2', '4', '8', '64', '1024');
		$params1['sale_type'] = "^='reversal'";
	}elseif($type == 'inv'){
		$GLOBALS['selector']->tables = $GLOBALS['select_tables1'];
		$params1['order_status'] = array('1', '2', '4', '8', '16', '32', '64', '1024');
	}
	
	//echo '<br>OPT<br>';
	//print_r($options);
	//print_r($params);
	$params = array_merge($params1, $params);
	$options = array_merge($options1, $options);

	//if(me()){print_r($GLOBALS['selector']->get_data($params, $options, 'query'));}
	$tmp_res = $GLOBALS[selector]->get_data($params, $options, $ret);
	$GLOBALS['selector']->tables = $tmp_selector_tables;

	if($ret == 'query'){
		return $tmp_res;
	}
	
	if($tmp_res['result']){
		foreach($tmp_res['result'] as $k => $v){
			$res[$v['payment_currency']] = $v;
		}
	}

	return $res;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////

function getGeneralResults($params = array(), $options = array(), $ret = ''){
	$tmp_selector_tables = $GLOBALS['selector']->tables;
	$GLOBALS['selector']->tables = $GLOBALS['select_tables3'];
	
	$params1['reseller_id'] = $GLOBALS['id'];
	$params1['client_id'] = '^IS NOT NULL';
	$params1['order_status'] = array('1', '2', '4', '8', '16', '32', '64', '1024');
	$options1['group_by'] = array('order_id', 'sale_date', 'sale_date_', 'sale_type', 'client_id', 'client_name', 'client_username', 'sum_amount_total', 'sum_amount_wholesale', 'payment_currency');
	$options1['order_by'] = 'sale_date_';
	$options1['order_by_'] = 'DESC';
	$options1['limit'] = 10;
	$options1['offset'] = 0;

	$params = array_merge($params1, $params);
	$options = array_merge($options1, $options);

	$result = $GLOBALS[selector]->get_data($params, $options, $ret);
	$GLOBALS['selector']->tables = $tmp_selector_tables;
	return $result;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////

function getStores($reseller_id){
	$local_connect = $GLOBALS['master_db'];
	$query = "SELECT store_id, title FROM stores WHERE reseller_id=$reseller_id";
	$local_connect->query($query);
	if($local_connect->error){
		return false;
	}
	return $local_connect->getAll();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////

function getMinYear($reseller_id){
	$local_connect = $GLOBALS['master_db'];
	$query = "SELECT min(substring(date from 1 for 4)) FROM orders WHERE reseller_id=$reseller_id AND order_status IN(1, 2, 4, 8, 16, 32, 64, 1024)";
	$GLOBALS['master_db']->query($query);
	$min_year = $GLOBALS['master_db']->getNext();
	return $min_year['min'];
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////

function getClientDomains($client_id, $params = array(), $options = array(), $ret = ''){
	$tmp_selector_tables = $GLOBALS['selector']->tables;
	$tmp_select_map = $GLOBALS['selector']->map;
	
	$GLOBALS['selector']->map = $GLOBALS['select_map2'];
	$GLOBALS['selector']->tables = $GLOBALS['select_tables2'];
	
	$params1['client_id'] = $client_id;
	$params1['registerdomain_id'] = '^IS NOT NULL';
	$options1['group_by'] = array('purchase_id', 'registerdomain_id', 'domain', 'expire_date', 'domain_flags', 'domain_ns1', 'domain_ns2', 'product_name', 'product_type', 'product_wholesale', 'product_retail');
	$options1['order_by'] = 'purchase_id';
	$options1['order_by_'] = 'DESC';
	$options1['limit'] = 1000;
	$options1['offset'] = 0;
	
	$params = array_merge($params1, $params);
	$options = array_merge($options1, $options);

	$result = $GLOBALS[selector]->get_data($params, $options, $ret);
	$GLOBALS['selector']->tables = $tmp_selector_tables;
	$GLOBALS['selector']->map = $tmp_select_map;
	return $result;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////

function clientDetails($client_id){
	$local_connect = $GLOBALS['master_db'];
	$query = "SELECT cl.client_id,cl.server,cl.username,cl.email,cl.firstname,cl.lastname,cl.address1,cl.address2,cl.city,cl.state,cl.zip,cl.country,cl.phone,cl.company_name,cn.name as country_name, p.purchase_id, p.product_id, p.period, p.expire_date, p.start_date, pr.name as product_name, pr.type as product_type FROM clients cl LEFT JOIN countries cn ON(cl.country = cn.iso2) LEFT JOIN purchases p ON (cl.client_id=p.client_id) LEFT JOIN products pr ON (p.product_id=pr.product_id) WHERE cl.client_id=$client_id AND pr.type='plan'";
	$local_connect->query($query);
	$client_details = $local_connect->getNext();
	return $client_details;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////

function loginClient($client){
	$userName = $GLOBALS['userName'];
	$word=md5(uniqid(rand(),1));
	$secret_word='as4dfSD5fdsadf';
	$secret=urlencode((crypt($client.$userName.$word.$secret_word)));
	$url = "http://www.supremeserver1.com/resellers/login/redirect_login.php?username=$client&reseller=$userName&word=$word&secret=$secret";
	return $url;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////

function getReports($info){
	$local_connect = $GLOBALS['master_db'];
	$other_pm = $GLOBALS['payment_methods']['other'];
	$query = "SELECT country_iso2 FROM vats WHERE eu=true";
	$local_connect->query($query);
	$eu_countries_arr = array_values($local_connect->getAll());
	foreach($eu_countries_arr as $k => $v){
		$eu_countries[] = $v['country_iso2'];
	}

	$params['store_id'] = $info['store_id'];
	$params['payment_currency'] = $info['payment_currency'];
	$params['payment_method'] = $info['payment_method'];

	if($params['payment_method'][10] == 'other'){
		unset($params['payment_method'][10]);
		$params['payment_method'] = array_merge($params['payment_method'], $other_pm);
	}

	if($info['client_individual'] xor $info['client_business']){
		$params['client_individual'] = $info['client_individual'];
		$params['client_business'] = $info['client_business'];
	}

	if($info['client_business_vat'] xor $info['client_business_novat']){
		$params['client_business_vat'] = $info['client_business_vat'];
		$params['client_business_novat'] = $info['client_business_novat'];
	}

	if($info['period_type'] == 'monthyear' and ($info['month'] or $info['year'])){
		$params['sale_date'] = '%'.$info['year'].'-'.$info['month'].'-';
	}elseif($info['period_type'] == 'customperiod' and ($info['edate'] or $info['sdate'])){
		if($info['edate']){
			$params['sale_date'][] = "^<='$info[edate]'";
		}
		if($info['sdate']){
			$params['sale_date'][] = "^>='$info[sdate]'";
		}
	}

	if($info['client_country'] == 'noneuclient'){
		if(preg_match('/^[A-Z]{2}$/', $info[$info['client_country']])){
			$params['client_country'] = $info[$info['client_country']];
		}else{
			$params['client_country'] = "^NOT IN ('".implode("','", $eu_countries)."')";
		}
	}elseif($info['client_country'] == 'euclient'){
		if(preg_match('/^[A-Z]{2}$/', $info[$info['client_country']])){
			$params['client_country'] = $info[$info['client_country']];
		}else{
			$params['client_country'] = "^IN ('".implode("','", $eu_countries)."')";
		}
	}

	//print_r($params);
	$options['limit'] = 10000;
	$options['offset'] = 0;
	
	$options['group_by'] = array('count', 'payment_currency', 'sum_amount_total', 'sum_amount_wholesale', 'sum_amount_total_usd', 'sum_amount_wholesale_usd', 'payment_method', 'client_individual', 'client_business', 'client_business_vat', 'client_business_novat');
	$cn_query =  getByCurr('cn', $params, $options, 'query');
	$local_connect->query($cn_query);
	$cn = $local_connect->getAll();
	
	if($params['sale_date']){
		$params['order_date'] = $params['sale_date'];
		unset($params['sale_date']);
	}

	$options['group_by'] = array('count', 'payment_currency', 'sum_order_amount_total', 'sum_order_amount_wholesale', 'sum_order_amount_total_usd', 'sum_order_amount_wholesale_usd', 'payment_method', 'client_individual', 'client_business', 'client_business_vat', 'client_business_novat');
	$inv_query = getByCurr('inv', $params, $options, 'query');
	$local_connect->query($inv_query);
	$inv = $local_connect->getAll();

	foreach($inv as $k => $v){
		if($v['client_individual'] == 't'){
			$type = 'individual';
		}else{
			if($v['client_business_vat'] == 't'){
				$type = 'business_vat';
			}else{
				$type = 'business_novat';
			}
		}
		
		if(in_array($v['payment_method'], $other_pm)){
			$pm = 'other';
			$res[$pm][$type][$v['payment_currency']]['inv']['count'] += $v['count'];
			$res[$pm][$type][$v['payment_currency']]['inv']['total'] += $v['sum_order_amount_total'];
			$res[$pm][$type][$v['payment_currency']]['inv']['wholesale'] += $v['sum_order_amount_wholesale'];
			$res[$pm][$type][$v['payment_currency']]['inv']['total_usd'] += $v['sum_order_amount_total_usd'];
			$res[$pm][$type][$v['payment_currency']]['inv']['wholesale_usd'] += $v['sum_order_amount_wholesale_usd'];
		}else{
			$pm = $v['payment_method'];
			$res[$pm][$type][$v['payment_currency']]['inv']['count'] = $v['count'];
			$res[$pm][$type][$v['payment_currency']]['inv']['total'] = $v['sum_order_amount_total'];
			$res[$pm][$type][$v['payment_currency']]['inv']['wholesale'] = $v['sum_order_amount_wholesale'];
			$res[$pm][$type][$v['payment_currency']]['inv']['total_usd'] = $v['sum_order_amount_total_usd'];
			$res[$pm][$type][$v['payment_currency']]['inv']['wholesale_usd'] = $v['sum_order_amount_wholesale_usd'];
		}		
	}
	//print_r($inv_query);

	foreach($cn as $k => $v){
		if($v['client_individual'] == 't'){
			$type = 'individual';
		}else{
			if($v['client_business_vat'] == 't'){
				$type = 'business_vat';
			}else{
				$type = 'business_novat';
			}
		}

		if(in_array($v['payment_method'], $other_pm)){
			$pm = 'other';
			$res[$pm][$type][$v['payment_currency']]['cn']['count'] += $v['count'];
			$res[$pm][$type][$v['payment_currency']]['cn']['total'] += $v['sum_amount_total'];
			$res[$pm][$type][$v['payment_currency']]['cn']['wholesale'] += $v['sum_amount_wholesale'];
			$res[$pm][$type][$v['payment_currency']]['cn']['total_usd'] += $v['sum_amount_total_usd'];
			$res[$pm][$type][$v['payment_currency']]['cn']['wholesale_usd'] += $v['sum_amount_wholesale_usd'];
		}else{
			$pm = $v['payment_method'];
			$res[$pm][$type][$v['payment_currency']]['cn']['count'] = $v['count'];
			$res[$pm][$type][$v['payment_currency']]['cn']['total'] = $v['sum_amount_total'];
			$res[$pm][$type][$v['payment_currency']]['cn']['wholesale'] = $v['sum_amount_wholesale'];
			$res[$pm][$type][$v['payment_currency']]['cn']['total_usd'] = $v['sum_amount_total_usd'];
			$res[$pm][$type][$v['payment_currency']]['cn']['wholesale_usd'] = $v['sum_amount_wholesale_usd'];
		}
	}
	//print_r($cn_query);

	return $res;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////

function format_money($amount, $currency){
	return ($GLOBALS['currencies'][$currency].' '.number_format($amount, 2, ',', ' '));
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////

function get_persentage($part, $total){
	return number_format(round(bcdiv($part, $total, 10) * 100, 2), 2, '.', '');
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////

function me(){
	if($_SERVER['REMOTE_ADDR'] == '88.203.210.112'){
		return true;
	}
	return false;
}

?>